package com.szgr.baoyin.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.AptitudeCheckActivity;
import com.szgr.baoyin.activity.DYCardActivity;
import com.szgr.baoyin.activity.DYCardDetailActivity;
import com.szgr.baoyin.activity.FinanceOperatorActivity;
import com.szgr.baoyin.activity.LoginActivity;
import com.szgr.baoyin.activity.MyAccountActivity;
import com.szgr.baoyin.activity.MyForumActivity;
import com.szgr.baoyin.activity.MyMoneyActivity;
import com.szgr.baoyin.activity.MyOrderActivity;
import com.szgr.baoyin.activity.MyProtectActivity;
import com.szgr.baoyin.activity.MySaveActivity;
import com.szgr.baoyin.activity.RedPacketActivity;
import com.szgr.baoyin.activity.ReturnGoodsActivity;
import com.szgr.baoyin.adapter.PersonalMessageListAdapter;
import com.szgr.baoyin.myview.NoScrollListView;
import com.szgr.baoyin.pull_to_zoom_scroll_view.PullToZoomScrollViewEx;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.DialogUtils;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.zxing.MipcaActivityCapture;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的
 * Created by Jimbo on 2015/9/16.
 */
public class PersonalFragment1203 extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener {

    private List<Map<String, Object>> data;
    private ImageView img_icon;
    private TextView tv_name;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.REQUEST_SUCCESS:
                    DialogUtils.hideLoadingDialog();
                    HashMap map = (HashMap) msg.obj;
                    MyApplication.getmInstance().setUsericon((String) map.get("user_icon"));
                    MyApplication.getmInstance().setUsername((String) map.get("user_name"));
                    tv_name.setText((String) map.get("user_name"));
                    MyTool.showPicture1(getActivity(), img_icon, (String) map.get("user_icon"));

                    JSONObject order_count = (JSONObject) map.get("order_count");
                    try {
                        String aNew = order_count.getString("new");
                        String receive = order_count.getString("receive");
                        String evaluation = order_count.getString("evaluation");
                        String refund = order_count.getString("refund");

                        if(!"0".equals(aNew)) {
                            tv_payNum.setVisibility(View.VISIBLE);
                            tv_payNum.setText(aNew);
                        }else{
                            tv_payNum.setVisibility(View.GONE);
                        }

                        if(!"0".equals(receive)) {
                            tv_getNum.setVisibility(View.VISIBLE);
                            tv_getNum.setText(receive);
                        }else{
                            tv_getNum.setVisibility(View.GONE);
                        }

                        if(!"0".equals(evaluation)) {
                            tv_judgeNum.setVisibility(View.VISIBLE);
                            tv_judgeNum.setText(evaluation);
                        }else{
                            tv_judgeNum.setVisibility(View.GONE);
                        }

                        if(!"0".equals(refund)) {
                            tv_leaveNum.setVisibility(View.VISIBLE);
                            tv_leaveNum.setText(refund);
                        }else{
                            tv_leaveNum.setVisibility(View.GONE);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    tv_duyinBao.setText((String)map.get("card_count"));
                    tv_coinBao.setText((String)map.get("point"));
                    tv_redBao.setText((String)map.get("voucher_count"));
                    break;
                case Constant.REQUEST_FAIL:
                    tv_payNum.setVisibility(View.GONE);
                    tv_getNum.setVisibility(View.GONE);
                    tv_judgeNum.setVisibility(View.GONE);
                    tv_leaveNum.setVisibility(View.GONE);

                    tv_duyinBao.setText("--");
                    tv_bankBao.setText("--");
                    tv_coinBao.setText("--");
                    tv_redBao.setText("--");
                    break;
            }
        }
    };
    private TextView tv_payNum;
    private TextView tv_getNum;
    private TextView tv_judgeNum;
    private TextView tv_leaveNum;
    private TextView tv_duyinBao;
    private TextView tv_bankBao;
    private TextView tv_coinBao;
    private TextView tv_redBao;

    public PersonalFragment1203() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_personal_copy1203, container, false);
        initView(ret);
        return ret;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(MyApplication.getmInstance().getUsericon() == null) {
            tv_name.setText("");
            img_icon.setImageResource(R.drawable.head_view);
            getUserMessage();
        }else{
            tv_name.setText(MyApplication.getmInstance().getUsername());
            MyTool.showPicture(getActivity(), img_icon,MyApplication.getmInstance().getUsericon());
            getUserMessage();
        }
    }

    private void initView(View ret) {
        data = new ArrayList<>();
        initListView();

        PullToZoomScrollViewEx pzsv = (PullToZoomScrollViewEx) ret.findViewById(R.id.pull_to_zoom_scroll_view);
        View imageView = new ImageView(getActivity());
        imageView.setBackgroundResource(R.drawable.mybg);
        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_pull_to_zoom_head_view, null, false);
        img_icon = (ImageView) headView.findViewById(R.id.head_view);
        tv_name = (TextView) headView.findViewById(R.id.head_name);

        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_pull_to_zoom_content_view, null, false);
        pzsv.setZoomView(imageView);
        pzsv.setHeaderView(headView);
        pzsv.setScrollContentView(contentView);

        NoScrollListView nslv_personalMessage = (NoScrollListView) contentView.findViewById(R.id.my_list_view);
        PersonalMessageListAdapter listAdapter = new PersonalMessageListAdapter(getActivity(), data);
        nslv_personalMessage.setAdapter(listAdapter);
        nslv_personalMessage.setOnItemClickListener(this);

        TextView tv_accountAddress = (TextView) headView.findViewById(R.id.zoom_account_address);
        tv_accountAddress.setOnClickListener(this);
        TextView tv_allOrder = (TextView) contentView.findViewById(R.id.zoom_all_order);
        tv_allOrder.setOnClickListener(this);
        TextView tv_toPay = (TextView) contentView.findViewById(R.id.zoom_to_pay);
        TextView tv_toGet = (TextView) contentView.findViewById(R.id.zoom_to_get);
        TextView tv_toJudge = (TextView) contentView.findViewById(R.id.zoom_to_judge);
        TextView tv_toLeave = (TextView) contentView.findViewById(R.id.zoom_to_leave);
        tv_toPay.setOnClickListener(this);
        tv_toGet.setOnClickListener(this);
        tv_toJudge.setOnClickListener(this);
        tv_toLeave.setOnClickListener(this);
        contentView.findViewById(R.id.zoom_douyin_card).setOnClickListener(this);
        contentView.findViewById(R.id.zoom_bank_card).setOnClickListener(this);
        contentView.findViewById(R.id.zoom_douyin_coin).setOnClickListener(this);
        contentView.findViewById(R.id.zoom_hong_bao).setOnClickListener(this);

        tv_payNum = (TextView) contentView.findViewById(R.id.pay_num);
        tv_getNum = (TextView) contentView.findViewById(R.id.get_num);
        tv_judgeNum = (TextView) contentView.findViewById(R.id.judge_num);
        tv_leaveNum = (TextView) contentView.findViewById(R.id.leave_num);
        tv_duyinBao = (TextView) contentView.findViewById(R.id.bao_duyin);
        tv_bankBao = (TextView) contentView.findViewById(R.id.bao_bank);
        tv_coinBao = (TextView) contentView.findViewById(R.id.bao_coin);
        tv_redBao = (TextView) contentView.findViewById(R.id.bao_red);
    }

    @Override
    public void onClick(View v) {
        if(MyApplication.getmInstance().isLogin()) {
            switch (v.getId()) {
                case R.id.zoom_account_address:
                    Intent accountIntent = new Intent(getActivity(), MyAccountActivity.class);
                    accountIntent.putExtra("username",MyApplication.getmInstance().getUsername());
                    accountIntent.putExtra("usericon",MyApplication.getmInstance().getUsericon());
                    startActivityForResult(accountIntent, 101);
                    break;
                case R.id.zoom_all_order:
                    Intent intent = new Intent(getActivity(), MyOrderActivity.class);
                    intent.putExtra("pos",0);
                    startActivity(intent);
                    break;
                case R.id.zoom_to_pay:
                    Intent intent1 = new Intent(getActivity(), MyOrderActivity.class);
                    intent1.putExtra("pos",1);
                    startActivity(intent1);
                    break;
                case R.id.zoom_to_get:
                    Intent intent2 = new Intent(getActivity(), MyOrderActivity.class);
                    intent2.putExtra("pos",3);
                    startActivity(intent2);
                    break;
                case R.id.zoom_to_judge:
                    Intent intent3 = new Intent(getActivity(), MyOrderActivity.class);
                    intent3.putExtra("pos",4);
                    startActivity(intent3);
                    break;
                case R.id.zoom_to_leave:
                    Intent intent4 = new Intent(getActivity(), MyOrderActivity.class);
                    intent4.putExtra("pos",2);
                    startActivity(intent4);
                    break;
                case R.id.zoom_douyin_card:
                    startActivity(new Intent(getActivity(), DYCardActivity.class));
                    break;
                case R.id.zoom_bank_card:
                    //TODO 银行卡
                    break;
                case R.id.zoom_douyin_coin:
                    startActivity(new Intent(getActivity(), MyMoneyActivity.class));
                    break;
                case R.id.zoom_hong_bao:
                    startActivity(new Intent(getActivity(), RedPacketActivity.class));
                    break;
            }
        }else{
            startActivity(new Intent(getActivity(),LoginActivity.class));
        }
    }

    private void initListView() {
        HashMap<String, Object> map = new HashMap<>();
//        map.put("icon", R.drawable.personal_01);
//        map.put("name", "绑定手机号");
//        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.my_00);
        map.put("name", "我的收藏");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.my_01);
        map.put("name", "资质审核");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.my_02);
        map.put("name", "我的维保");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.my_03);
        map.put("name", "消息中心");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.my_04);
        map.put("name", "我的论坛");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.my_04);
        map.put("name", "商务代表");
        data.add(map);

//        map = new HashMap<>();
//        map.put("icon", R.drawable.personal_06);
//        map.put("name", "消息中心");
//        data.add(map);
//
//        map = new HashMap<>();
//        map.put("icon", R.drawable.personal_02);
//        map.put("name", "我的论坛");
//        data.add(map);
//
//        map = new HashMap<>();
//        map.put("icon", R.drawable.personal_09);
//        map.put("name", "帐户安全");
//        data.add(map);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(MyApplication.getmInstance().isLogin()) {
            switch (position) {
                case 0:
                    startActivity(new Intent(getActivity(), MySaveActivity.class));
                    break;
                case 1:
                    startActivity(new Intent(getActivity(), AptitudeCheckActivity.class));
                    break;
                case 2:
                    startActivity(new Intent(getActivity(), MyProtectActivity.class));
                    break;
                case 3:

                    break;
                case 4:
                    startActivity(new Intent(getActivity(), MyForumActivity.class));
                    break;
                case 5:
                    startActivity(new Intent(getActivity(), FinanceOperatorActivity.class));
                    break;
            }
        }else{
            startActivity(new Intent(getActivity(),LoginActivity.class));
        }
    }

    /**
     * 获取用户信息
     */
    private void getUserMessage() {
        DialogUtils.showLoadingDialog(getActivity(),"正在加载中...");
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_MY_MESSAGE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("----->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            String code = jsonObject.getString("code");
                            if("200".equals(code) && jsonObject.has("datas")){
                                JSONObject datas = jsonObject.getJSONObject("datas");
                                JSONObject member_info = datas.getJSONObject("member_info");
                                String user_name = member_info.getString("member_truename");
                                if(user_name == null || "".equals(user_name)){
                                    user_name = member_info.getString("member_name");
                                }

                                String user_icon = member_info.getString("avator");

                                String point = member_info.getString("point");
                                String card_count = member_info.getString("card_count");
                                String voucher_count = member_info.getString("voucher_count");

                                JSONObject order_count = member_info.getJSONObject("order_count");


                                HashMap<String,Object> map = new HashMap<>();
                                map.put("user_name",user_name);
                                map.put("user_icon",user_icon);
                                map.put("point",point);
                                map.put("card_count",card_count);
                                map.put("voucher_count",voucher_count);
                                map.put("order_count",order_count);
                                mHandler.obtainMessage(Constant.REQUEST_SUCCESS,map).sendToTarget();
                            }else{
                                mHandler.obtainMessage(Constant.REQUEST_FAIL).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            mHandler.obtainMessage(Constant.REQUEST_FAIL).sendToTarget();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mHandler.obtainMessage(Constant.REQUEST_FAIL).sendToTarget();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }
}
